import Axios from 'axios';
import nProgress from 'nprogress';
import 'nprogress/nprogress.css';
const request = Axios.create({
  baseURL: '/dev-api',
  timeout: 10000,
});

request.interceptors.request.use(
  (config) => {
    nProgress.start();

    const token = localStorage.getItem('token');
    if (token) {
      // 如果有token数据,就将token写在请求头中进行发送
      config.headers.token = token;
    }

    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);

request.interceptors.response.use(
  (response) => {
    nProgress.done();

    return response.data;
  },
  (err) => {
    nProgress.done();

    return Promise.reject(err);
  }
);

export default request;
